const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const baseUrl = require('../controllers/controllers.js').baseUrl;
Page({
    data: {
        id: '',
        detail: '',
        desc: '',
        resondata: [{ name: "多拍" }, { name: "拍错" }, { name: "不想要" }, { name: "等太久了" }, { name: "不想再等了～" }],
        reson: '',
        graphimg: [],
        resonindex: "",
        topNavigationHeight: 0
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        this.setData({
            topNavigationHeight: app.globalData.topNavigationHeight + "rpx"
        })
    },
    onLoad(options) {
        if (options.id) {
            this.setData({
                id: options.id
            })
            this.orderdetail()
        }
        if (options.parentid) {
            app.globalData.parentid = options.parentid
        }
    },
    bindPickerChange: function(e) {
        this.setData({
            reson: this.data.resondata[e.detail.value].name,
            resonindex: e.detail.value
        })
    },
    orderdetail() {
        Controller.orderdetail({ orderNumber: this.data.id }).then(res => {
            if (res.data.baseData) {
                res.data.baseData.itemList.forEach(data => {
                    data.specsList = data.stockSpecs ? data.stockSpecs.split(',') : []
                })
                this.setData({
                    detail: res.data.baseData,
                })
            }
        })
    },
    changedesc(e) {
        this.setData({
            desc: e.detail.value
        })
    },
    changereson(e) {
        this.setData({
            reson: e.detail.value
        })
    },
    selectgraph: function(type) {
        var that = this;
        wx.chooseImage({
            count: 3,
            sizeType: ['original', 'compressed'],
            sourceType: ["album"],
            success: function(res) {
                var img = res.tempFilePaths[0];
                that.picture(img)
            },
            fail: function(res) {
                console.log(res)
            }
        })
    },
    picture(e) {
        var that = this;
        wx.uploadFile({
            url: baseUrl + '/upload/picture',
            filePath: e,
            name: "file",
            formData: {},
            success: function(res) {
                that.setData({
                    graphimg: that.data.graphimg.concat(JSON.parse(res.data).data.baseData[0]),
                })
            }
        })
    },
    delete(e) {
        var graphimg = this.data.graphimg;
        var index = e.currentTarget.dataset.index
        graphimg.splice(index, 1)
        this.setData({
            graphimg
        })
    },
    next() {
        if (!this.data.reson) {
            wx.showToast({
                title: "请输入退款原因",
                icon: "none"
            })
            return
        }
        // if(this.data.graphimg.length<=0){
        //   wx.showToast({
        //     title:"请上传凭证",
        //     icon:"none"
        //   })
        //   return
        // }
        var data = {
            orderNumber: this.data.id,
            remark: this.data.desc,
            action: this.data.reson,
            imgUrls: this.data.graphimg.join(','),
        }
        Controller.refundapply(data).then(res => {

            wx.showToast({
                title: "提交成功",
                icon: "none"
            })
            if (this.data.detail.refundStatus == 1) {
                setTimeout(() => {
                    wx.redirectTo({
                        url: '/pages/service/address?id=' + this.data.id
                    })
                }, 1000)
            } else {
                setTimeout(() => {
                    wx.redirectTo({
                        url: '/pages/Me/detail?id=' + this.data.id
                    })
                }, 1000)
            }

        })

    },
    onShareAppMessage: function(e) {
        let title = '';
        let imageUrl = '';
        let path = app.getCurrentPageUrlWithArgs()
        let obj = {
            title,
            imageUrl,
            path,
            success: function(res) {},
            fail(err) {}
        }
        return obj
    },
    onShareTimeline: function(res) {
        let title = '';
        let imageUrl = '';
        let path = app.getCurrentPageUrlWithArgs()
        let obj = {
            title,
            imageUrl,
            path,
            success: function(res) {},
            fail(err) {}
        }
        return obj
    },
})